package com.tuanzi.loan.web.bean.product.product;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.product.product.LoanProductInfo;
import com.tuanzi.loan.business.service.product.product.LoanProductInfoService;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.product.product.LoanProductInfoSearchRequest;
import com.tuanzi.loan.vo.response.product.product.LoanProductInfoSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingSessionBean;
import com.tuanzi.loan.web.lazy.product.product.LoanProductInfoLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanProductListBean extends JSFListingSessionBean<LoanProductInfoSearchRequest, LoanProductInfoSearchResponse> {

	private static final long serialVersionUID = -2152778554108785342L;
	@Autowired
	private LoanProductInfoService loanProductInfoService;
	@Autowired
	private DictionaryService dictionaryService;

	private Long totalDailyClickUvLimitation;
	private Long totalOnline;
	private Long totalAutoOnline;
	private Long totalDailyClickUvRemainingOnline;

	public void onReset() {
		initLazyDataModel();
	}

	public String onAdd() {
		return redirect("/views/product/product/info.xhtml");
	}

	public String onEdit(String id) {
		return redirect("/views/product/product/info.xhtml?id=" + id);
	}

	public String onImport() {
		return redirect("/views/product/product/import.xhtml");
	}

	private void loadTotal() {
		totalAutoOnline = loanProductInfoService.countAutoOnline(this.get$());
		totalOnline = loanProductInfoService.countOnline(this.get$());
		totalDailyClickUvLimitation = loanProductInfoService.sumDailyClickUvLimitation(this.get$());
		totalDailyClickUvRemainingOnline = loanProductInfoService.sumDailyClickUvRemainingByOnline(this.get$());
	}

	public void onSearch() {
		loadTotal();
	}

	public void onOnline(Long id) {
		loanProductInfoService.online(loanProductInfoService.findOne(id));
		addSuccessfullyMessage();
	}

	public void onOffline(Long id) {
		loanProductInfoService.offline(loanProductInfoService.findOne(id));
		addSuccessfullyMessage();
	}

	public void onDelete(Long id) {
		loanProductInfoService.sortDelete(loanProductInfoService.findOne(id));
	}

	public void onBatchOnline() {
		loanProductInfoService.online(getSelectedProductInfos());
		addSuccessfullyMessage();
	}

	public void onBatchOffline() {
		loanProductInfoService.offline(getSelectedProductInfos());
		addSuccessfullyMessage();
	}

	private List<LoanProductInfo> getSelectedProductInfos() {
		return loanProductInfoService
				.findAll(selectedRecords.parallelStream().map(LoanProductInfoSearchResponse::getId).map(x -> NumberUtils.toLong(x.toString())).collect(Collectors.toList()));
	}

	@Override
	protected String page() {
		return "/views/product/product/list.xhtml";
	}

	@Override
	protected void initLazyDataModel() {
		$ = new LoanProductInfoSearchRequest();
		lazyDataModel = new LoanProductInfoLazyDataModel($, session, loanProductInfoService);
		loadTotal();
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(ONLINE_OFFLINE_STATUS, RECOMMEND_POOL_TYPE,LARGE_COMMON_PRODUCT);
	}
}
